<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchObj" class="demo-form-inline">
      <el-form-item label="关键字">
        <el-autocomplete
          :trigger-on-focus="false"
          :fetch-suggestions="querySearch"
          v-model="searchObj.name"
          class="inline-input"
          placeholder="请输入内容"
          @select="handleSelect"
        />
        <!--<el-input v-model="searchObj.name" :fetch-suggestions="querySearchAsync" placeholder="关键字" @select="handleSelect"/>-->
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchObj.level" clearable placeholder="头衔">
          <el-option value="1" label="高级讲师"/>
          <el-option value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="加入时间">
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker
              v-model="searchObj.joinDateBegin"
              type="date"
              placeholder="开始日期"
              value-format="yyyy-MM-dd"
              style="width: 100%;"/>
          </el-form-item>
        </el-col>
        <el-col :span="2" class="line">-</el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-date-picker
              v-model="searchObj.joinDateEnd"
              type="date"
              placeholder="结束时间"
              value-format="yyyy-MM-dd"
              style="width: 100%;"/>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-button type="primary" @click="getTeachers">查询</el-button>

    </el-form>
    <h1>讲师管理</h1>
    <p>讲师列表</p>
    <el-row>
      <el-button type="danger" round @click="batchRemove">批量删除</el-button>
    </el-row>
    <el-table
      :data="teachers"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column
        label="讲师头像"
        width="180">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.avatar" :size="60" shape="square"/>
        </template>
      </el-table-column> -->
      <el-table-column
        type="selection"
        width="55"/>
      <el-table-column
        label="姓名"
      >
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>姓名: {{ scope.row.name }}</p>
            <p>讲师简介: {{ scope.row.intro }}</p>
            <p>讲师资历: {{ scope.row.career }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.name }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <!--<el-table-column-->
      <!--label=""-->
      <!--&gt;-->
      <!--<template slot-scope="scope">-->
      <!--<span style="margin-left: 10px">{{ scope.row.career }}</span>-->
      <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column
        label="头衔"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.level === 1"
            :type="scope.row.level === 1 ? 'primary' : 'success'"
            disable-transitions>高级讲师
          </el-tag>
          <el-tag
            v-if="scope.row.level === 2"
            :type="scope.row.level === 2 ? 'primary' : 'success'"
            disable-transitions>首席讲师
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="入驻时间"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.joinDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>

import teacherApi from '@/api/teacher.js'

export default {
  data() {
    return {
      teachers: [],
      fit: 'cover',
      pageSizes: [10, 20, 30, 40, 50, 100],
      list: null, // 数据列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询条件,
      multipleSelection: [], // 选中行数组,
      restaurants: [], // 讲师名称数组,
      state: ''
    }
  },
  created() {
    this.getTeachers()
  },
  methods: {
    getTeachers() {
      teacherApi.listPage(this.page, this.limit, this.searchObj).then(res => {
        this.teachers = res.data.page.records
        this.total = res.data.page.total
        this.restaurants = res.data.page.records
      })
    },
    handleEdit(index, row) {
      this.$router.push({ path: 'edit/' + row.id })
    },
    handleDelete(index, row) {
      console.log(row)
      this.$confirm('此操作将永久删除' + row.name + '讲师, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        teacherApi.removeById(row.id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getTeachers()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange(val) {
      this.limit = val
      this.getTeachers()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.page = val
      this.getTeachers()
      console.log(`当前页: ${val}`)
    },
    handleSelectionChange(val) {
      const map = val.map(function(item, index) {
        return item.id
      })
      console.log(`val`, map)
      this.multipleSelection = map
    },
    batchRemove() {
      this.$confirm('此操作将永久删除多位讲师, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        teacherApi.batchRemove(this.multipleSelection).then(res => {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.getTeachers()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    querySearch(queryString, cb) {
      teacherApi.listByKey({ 'key': queryString }).then(res => {
        this.restaurants = res.data.list
      })
      clearTimeout(this.timeout)
      this.timeout = setTimeout(() => {
        const map = this.restaurants.map(function(item, index) {
          return { value: item.name }
        })
        console.log(map)
        cb(map)
      }, 3000 * Math.random())
    },
    handleSelect(item) {
      console.log(item)
    }
  }
}
</script>
